package com.lzxuni.modules.dataAcquisition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.dataAcquisition.entity.*;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkProcessIndexService;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkProcessService;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkService;
import com.lzxuni.modules.dataAcquisition.service.ProductIndexService;
import com.lzxuni.modules.productdisplay.entity.ProductDisplay;
import com.lzxuni.modules.productdisplay.service.ProductDisplayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Information")
public class TraceabilityInformationController {

    @Autowired
    private ProductDisplayService productService;

    @Autowired
    private ChainLinkService linkService;

    @Autowired
    private ChainLinkProcessService processService;

    @Autowired
    private ChainLinkProcessIndexService indexService;

    @Autowired
    private ProductIndexService productIndexService;

    @Autowired
    private FileEntityService fileEntityService;

    @RequestMapping("/getChains")
    public Object getChains(String productId) throws Exception {
        ProductDisplay product = productService.getById(productId);
        List<ChainLink> links = new ArrayList<>();
        if (product != null) {
            String chainId = product.getTraceabilityChain();
            links = linkService.list(new QueryWrapper<ChainLink>().eq("chain_id", chainId).orderByAsc("create_time"));
            if (links != null && links.size() > 0) {
                for (ChainLink link : links) {
                    List<ChainLinkProcess> processes = processService.list(new QueryWrapper<ChainLinkProcess>().eq("link_id", link.getId()).orderByAsc("create_time"));
                    if (processes != null && processes.size() > 0) {
                        for (ChainLinkProcess process : processes) {
                            List<ChainLinkProcessIndex> indices = indexService.list(new QueryWrapper<ChainLinkProcessIndex>().eq("process_id", process.getId()).orderByAsc("create_time"));
                            if (indices != null && indices.size() > 0) {
                                for (ChainLinkProcessIndex index : indices) {
                                    ProductIndex productIndex = productIndexService.getOne(new QueryWrapper<ProductIndex>().eq("product_id", productId).eq("index_id", index.getId()));
                                    if (productIndex != null) {
                                        if(index.getCollectionType().equals("视频")){
                                            FileEntity fileEntity = new FileEntity();
                                            fileEntity.setYwId(productIndex.getProductIndexId());
                                            fileEntity.setYwType("indexFile");
                                            List<FileEntity> fileEntities = fileEntityService.queryListByFileEntity(fileEntity);
                                            productIndex.setFileEntityList(fileEntities);
                                        }
                                        if(index.getCollectionType().equals("图片")){
                                            FileEntity fileEntity = new FileEntity();
                                            fileEntity.setYwId(productIndex.getProductIndexId());
                                            fileEntity.setYwType("indexPic");
                                            List<FileEntity> fileEntities = fileEntityService.queryListByFileEntity(fileEntity);
                                            productIndex.setFileEntityList(fileEntities);
                                        }
                                    }
                                    index.setProductIndex(productIndex);
                                }
                            }
                            process.setIndexList(indices);
                        }
                    }
                    link.setProcessList(processes);
                }
            }
        }
        return links;
    }

}
